package com.bringway.p2p.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bringway.p2p.dao.ZnDao;
import com.bringway.p2p.service.ZnService;

import net.sf.json.JSONObject;

@Service
@Transactional
public class ZnServiceImpl implements ZnService {

	@Autowired
	private ZnDao znDao;

	@Override
	public List queryYjHmcList(JSONObject params) {
		return znDao.queryYjHmcList(params);
	}

	@Override
	public int getYjHmcCount(JSONObject params) {
		return znDao.getYjHmcCount(params);
	}

	/**
	 * 默认只对runtimeexception回滚
	 * truncate 无法回滚
	 * */
	@Override
	@Transactional(value = "transactionManager_p2p",rollbackFor=Exception.class)
	public void importHmc(List<String[]> excelList, JSONObject params) {
		znDao.truncateHmc(params);
		znDao.importHmc(excelList,params);
		/*try{
			znDao.truncateHmc(params);
			znDao.importHmc(excelList,params);
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException("回滚...");
		}*/
		
	}

	@Override
	public JSONObject checkExcelTitle(XSSFSheet sheet) {
		JSONObject result = new JSONObject();
		XSSFRow titleRow = sheet.getRow(0);
        if(titleRow.getLastCellNum() < 10){
        	result.put("success", false);
        	result.put("msg", "对不起，您的操作不正确！列不能少于10");
        }else{
        	XSSFCell titleRowCell0 = titleRow.getCell(0);
        	XSSFCell titleRowCell1 = titleRow.getCell(1);
        	XSSFCell titleRowCell2 = titleRow.getCell(2);
        	XSSFCell titleRowCell3 = titleRow.getCell(3);
        	XSSFCell titleRowCell4 = titleRow.getCell(4);
        	XSSFCell titleRowCell5 = titleRow.getCell(5);
        	XSSFCell titleRowCell6 = titleRow.getCell(6);
        	XSSFCell titleRowCell7 = titleRow.getCell(7);
        	XSSFCell titleRowCell8 = titleRow.getCell(8);
        	XSSFCell titleRowCell9 = titleRow.getCell(9);
        	XSSFCell titleRowCell10 = titleRow.getCell(10);
        	if(!"序号".equals(titleRowCell0.getStringCellValue())){
        		result.put("success", false);
            	result.put("msg", "对不起，您的操作不正确！请核对第1列，列名：[序号]");
        	}else if(!"地区".equals(titleRowCell1.getStringCellValue())){
        		result.put("success", false);
            	result.put("msg", "对不起，您的操作不正确！请核对第2列，列名：[地区]");
        	}else if(!"部门".equals(titleRowCell2.getStringCellValue())){
        		result.put("success", false);
            	result.put("msg", "对不起，您的操作不正确！请核对第3列，列名：[部门]");
        	}else if(!"所属".equals(titleRowCell3.getStringCellValue())){
        		result.put("success", false);
            	result.put("msg", "对不起，您的操作不正确！请核对第4列，列名：[所属]");
        	}else if(!"职务".equals(titleRowCell4.getStringCellValue())){
        		result.put("success", false);
            	result.put("msg", "对不起，您的操作不正确！请核对第5列，列名：[职务]");
        	}else if(!"职级".equals(titleRowCell5.getStringCellValue())){
        		result.put("success", false);
            	result.put("msg", "对不起，您的操作不正确！请核对第6列，列名：[职级]");
        	}else if(!"员工编码".equals(titleRowCell6.getStringCellValue())){
        		result.put("success", false);
            	result.put("msg", "对不起，您的操作不正确！请核对第7列，列名：[员工编码]");
        	}else if(!"姓名".equals(titleRowCell7.getStringCellValue())){
        		result.put("success", false);
            	result.put("msg", "对不起，您的操作不正确！请核对第8列，列名：[姓名]");
        	}else if(!"入职时间".equals(titleRowCell8.getStringCellValue())){
        		result.put("success", false);
            	result.put("msg", "对不起，您的操作不正确！请核对第9列，列名：[入职时间]");
        	}else if(!"转正日期".equals(titleRowCell9.getStringCellValue())){
        		result.put("success", false);
            	result.put("msg", "对不起，您的操作不正确！请核对第10列，列名：[转正日期]");
        	}else if(!"转正提醒".equals(titleRowCell10.getStringCellValue())){
        		result.put("success", false);
            	result.put("msg", "对不起，您的操作不正确！请核对第11列，列名：[转正提醒]");
        	}else{
        		result.put("success", true);
            	result.put("msg", "表头验证通过");
        	}
        }
        return result;
	}

	@Override
	@Transactional(value = "transactionManager_p2p",rollbackFor=Exception.class)
	public void delHmcUser(String id) {
		znDao.delHmcUser(id);
	}

	@Override
	public Map<String, String> checkParamsNotNull(JSONObject params) {
		Map<String, String> result = new HashMap<>();
		if(null != params){
			result.put("checkResult", "true");
			result.put("msg", "初始验证通过！");
		}
		Iterator it =  params.keys();
		while (it.hasNext()) {  
            String key = (String) it.next();  
            String value = params.getString(key);
            if(StringUtils.isEmpty(value)){
            	result.put("checkResult", "false");
            	result.put("msg", key + "不能为空！");
            	break;
            }
        }  
		return result;
	}

	@Override
	public void addHmcUser(JSONObject params) {
		znDao.addHmcUser(params);
	}

	@Override
	public boolean checkGyh(JSONObject params) {
		return znDao.checkGyh(params);
	}
	
	
}
